﻿using BenCaoXuan.Infrastructure.Attr;
using BenCaoXuan.Infrastructure.Dto;
using BenCaoXuan.Infrastructure.Dto.Admin.employee;
using BenCaoXuan.Infrastructure.Tools;
using BenCaoXuan.Service.Instance.admin;
using BenCaoXuan.Service.Instance.middle;
using BenCaoXuan.Service.Instance.PC;
using BenCaoXuan.Service.Interface.admin;
using BenCaoXuan.Service.Interface.middle;
using BenCaoXuan.Service.Interface.PC;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace BenCaoXuan.Api.Admin.Controllers.pc
{
    [ApiGroup("PC(桌面端模块)")]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PcController : ControllerBase
    {
        private readonly IConfiguration _configuration;
        private readonly I_PcLogin_Service _pcLogin_Service;

        public PcController(IConfiguration configuration, I_PcLogin_Service pcLogin_Service)
        {
            _configuration = configuration;
            _pcLogin_Service = pcLogin_Service;
        }

        //public LoginController(IConfiguration configuration, I_Login_Service login_Service) :base(login_Service)
        //{
        //}

        /// <summary>
        /// 获取当前登录桌面端的用户
        /// </summary>
        /// <returns></returns>
        protected Employee_Dto Get_PC_User()
        {
            var code = Response.HttpContext.User.Identity?.Name;

            return _pcLogin_Service.Check_Pc_Login(code!, "");

        }

        /// <summary>
        /// 桌面端-登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Logins(Login_Dto dto)
        {
            dto.code = Config.GUID2();
            var data = await _pcLogin_Service.Logins(dto);
            if (data.code == Api_Code.ok)
            {
                var claims = new[]
                    {
                    new Claim(ClaimTypes.Name,  dto.code),
                    new Claim(ClaimTypes.Actor,  dto.account!),
                    new Claim(ClaimTypes.Role,"pc")//角色设定
                };
                var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWT:SecurityKey"]));
                var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
                string jwtKey = _configuration["JWT:issuer"];
                var token = new JwtSecurityToken(
                    issuer: jwtKey,
                    audience: jwtKey,
                    claims: claims,
                    expires: DateTime.Now.AddMinutes(60 * 12), //token 过期时间
                    signingCredentials: creds);

                data.data = new
                {
                    token = new JwtSecurityTokenHandler().WriteToken(token),
                };

            }

            return Ok(data);
        }


        /// <summary>
        /// 检查登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize(Roles = "pc")]
        public IActionResult Check_login()
        {
            var user = this.Get_PC_User();
            if (user == null)
            {
                return Unauthorized();//401
            }

            return Ok(new Api_Result_Dto()
            {
                code = 0,
                message = "ok",
                data = new
                {
                    name = user.name,
                    photo = user.photo

                }
            });
        }


    }
}
